define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.dns_id == '' || Config.dns_id == null){
        Config.dns_id = 0;
    }
    if(Config.type_id == '' || Config.type_id == null){
        Config.type_id = 0;
    }
    if(Config.construction_id == '' || Config.construction_id == null){
        Config.construction_id = 0;
    }
    var Controller = {
        index: function () {
            //解析域名
            $(document).on('click', '.btn-analysis', function () {
                Layer.alert('解析完成');
                // $a = $('#table').bootstrapTable('getAllSelections');
                // Layer.alert(JSON.stringify(Table.api.selecteddata(table)));
                // Fast.api.ajax({
                //     url: "dnsrecord/analysis",
                //     dataType: 'jsonp',
                //     data: {
                //         account: 1,
                //         password: 2,
                //         _method: 'POST'
                //     }
                // }, function (data, ret) {
                //     Layer.closeAll();
                //     Layer.alert('解析完成');
                //     // Layer.alert(ret.msg);
                // }, function (data, ret) {
                //
                // });
            });
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dnslist/index/?dns_id=' + Config.dns_id + '&type_id=' + Config.type_id + '&construction_id=' + Config.construction_id,
                    add_url: 'dnslist/add',
                    edit_url: 'dnslist/edit',
                    del_url: 'dnslist/del',
                    multi_url: 'dnslist/multi',
                    import_url: 'dnslist/import',
                    table: 'dns_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'label', title: __('标签'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'热门': 'info', 'VIP':'danger','经典':'success','付费':'warning'}},
                        {field: 'api_type_name', title: __('域名类型'), operate: 'LIKE'},
                        // {field: 'construction_name', title: __('Construction_id'), operate: false},
                        // {field: 'system_name', title: __('TypeId'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {
                        //     field: 'buttons',
                        //     width: "120px",
                        //     title: __('查看下级域名'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [
                        //         {
                        //             name: 'detail',
                        //             text: __('查看下级域名'),
                        //             title: __('查看下级域名'),
                        //             classname: 'btn btn-sm btn-primary btn-dialog',
                        //             icon: 'fa fa-leaf',
                        //             url: 'dnslist/index',
                        //             visible:function (row) {
                        //                 if(row.dns_id == 0){
                        //                     return true;
                        //                 }else{
                        //                     return false;
                        //                 }
                        //             }
                        //         }
                        //     ],
                        //     formatter: Table.api.formatter.buttons
                        // },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('域名解析'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('添加域名解析'),
                                    title: __('添加域名解析'),
                                    classname: 'btn btn-sm btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'dnsrecord/add'
                                },
                                // {
                                //     name: 'detail',
                                //     text: __('域名解析记录'),
                                //     title: __('域名解析记录'),
                                //     classname: 'btn btn-sm btn-warning btn-dialog',
                                //     icon: 'fa fa-list',
                                //     url: 'dnsrecord/index'
                                // },
                                {
                                    name: 'detail',
                                    text: __('域名操作记录'),
                                    title: __('域名操作记录'),
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'dnsanalysis/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作记录'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('操作记录'),
                                    title: __('操作记录'),
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'feditionlog/index/?type=2'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //导入godaddy活跃域名
            $(document).on('click', '.btn-godaddy', function () {
                $.ajax({
                    dataType: 'json',
                    type: 'POST',
                    url: "dnslist/godaddy",
                    data: {},
                    success: function (json) {
                        console.log('json',json);
                        Layer.closeAll();
                        Layer.alert('解析完成');
                        //刷新父级页面
                        window.parent.location.reload();
                        //刷新当前页面
                        table.bootstrapTable('refresh', {});
                        return true;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        Layer.alert('解析失败');
                    }
                });
            });

            //导入namesilo活跃域名
            $(document).on('click', '.btn-namesilo', function () {
                $.ajax({
                    dataType: 'json',
                    type: 'POST',
                    url: "dnslist/namesilo",
                    data: {},
                    success: function (json) {
                        console.log('json',json);
                        Layer.closeAll();
                        Layer.alert('解析完成');
                        //刷新父级页面
                        window.parent.location.reload();
                        //刷新当前页面
                        table.bootstrapTable('refresh', {});
                        return true;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        Layer.alert('解析失败');
                    }
                });
            });

            //导入namecheap活跃域名
            $(document).on('click', '.btn-namecheap', function () {
                $.ajax({
                    dataType: 'json',
                    type: 'POST',
                    url: "dnslist/namecheap",
                    data: {},
                    success: function (json) {
                        console.log('json',json);
                        Layer.closeAll();
                        Layer.alert('解析完成');
                        //刷新父级页面
                        window.parent.location.reload();
                        //刷新当前页面
                        table.bootstrapTable('refresh', {});
                        return true;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        Layer.alert('解析失败');
                    }
                });
            });

            // 添加域名标签
            $(document).on("click", ".btn-dnslabel", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("dnslabel/add?name=" + name, __('添加域名标签'));
            });
        },
        add: function () {
            // 监听下拉列表改变的事件
            $("#c-dns_type").bind("change",function () {
                if (this.value== 1){
                    $('#hide_dns_name').hide();
                    $('#hide_dns_name2').show();
                    $('#hide_dns_id').show();
                }else{
                    $('#hide_dns_name').show();
                    $('#hide_dns_name2').hide();
                    $('#hide_dns_id').hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});